package com.wzy.action;

import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.ServletActionContext;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

public class UploadAction extends ActionSupport {

    //封装上传文件属性
    private File upload;
    //获取提交的文件类型
    private String uploadContentType;
    //封装上传文件名称
    private String uploadFileName;


    //获取文件上传的路径
    private String savePath ;

    @Override
    public String execute() throws Exception {
        byte[] buffer = new byte[1024];
        FileInputStream fis = new FileInputStream(this.getUpload());
        FileOutputStream fos = new FileOutputStream(getSavePath() + "\\" + this.getUploadFileName());
        int length = fis.read(buffer);
        while (length > 0) {
            //每次写入length长度的内容
            fos.write(buffer, 0, length);
            length = fis.read(buffer);
        }
        fis.close();
        fos.flush();
        fos.close();
        return SUCCESS;
    }

    public File getUpload() {
        return upload;
    }

    public void setUpload(File upload) {
        this.upload = upload;
    }

    public String getUploadContentType() {
        return uploadContentType;
    }

    public void setUploadContentType(String uploadContentType) {
        this.uploadContentType = uploadContentType;
    }

    public String getUploadFileName() {
        return uploadFileName;
    }

    public void setUploadFileName(String uploadFileName) {
        this.uploadFileName = uploadFileName;
    }

    public String getSavePath() {
       return ServletActionContext.getServletContext().getRealPath(savePath);
    }

    public void setSavePath(String savePath) {
        this.savePath = savePath;
    }
}
